#include <logbase.h>
/*
栈：先进后出。
|进出口|
|5|
|4|
|3|
|2|
|1|
|0|栈底
|-|//不能出
*/

// 这里做一个int型的栈。

struct StackInformSturct
{
    int size;//栈最大长度
    int count;//入栈数据个数。
    int *buffer;//保存栈数据
};


int freeStack(struct StackInformSturct *stack)
{
    if(!stack)//指针要判断空
    {
        logerrorc("");
        return -1;
    }
    if(stack->buffer)
    {
        free(stack->buffer);
        stack->buffer = NULL;
    }
    stack->size = 0;
    stack->count = 0;
    return 0;
}

int initStack(struct StackInformSturct *stack, int setsize)
{
    if(!stack)//指针要判断空
    {
        logerrorc("");
        return -1;
    }
    if(!(setsize > 0 && setsize < 10000))//长度判断范围
    {
        logerrorc("%d", setsize);
        return -1;
    }
    freeStack(stack);//先释放。
    stack->buffer = (int *)malloc(sizeof(int) * setsize);
    if(stack->buffer == NULL)
    {
        logerrorc("");
        return -1;
    }
    stack->size = setsize;
    stack->count = 0;
    return stack->size;
}

int inputStack(struct StackInformSturct *stack, int value)
{
    if(!stack)//指针要判断空
    {
        logerrorc("");
        return -1;
    }
    if(!(stack->count < stack->size))
    {
        logerrorc("size=%d,count=%d", stack->size, stack->count);
        return -1;
    }
    stack->buffer[stack->count++] = value;
    return 0;
}

int getStack(struct StackInformSturct *stack, int *value)
{
    if(!stack)//指针要判断空
    {
        logerrorc("");
        return -1;
    }
    if(!value)
    {
        logerrorc("");
        return -1;
    }
    if(!((stack->count > 0) && (stack->count <= stack->size)))
    {
        logerrorc("size=%d,count=%d", stack->size, stack->count);
        return -1;
    }
    value[0] = stack->buffer[--stack->count];
    return 0;
}

#define STACK_CHECK_SIZE 10
int main(int argc, char **argv)
{
    {
        struct StackInformSturct tmpstack;//未初始化
        logdebugc("%d,%d,%p", tmpstack.size, tmpstack.count, tmpstack.buffer);
    }
    {
        struct StackInformSturct tmpstack = {};//初始化
        logdebugc("%d,%d,%p", tmpstack.size, tmpstack.count, tmpstack.buffer);
    }
    struct StackInformSturct tmpstack = {};
    int tmpvalue = 0;
    initStack(&tmpstack, STACK_CHECK_SIZE);

    for(int i = 0; i < STACK_CHECK_SIZE; i++)
    {
        inputStack(&tmpstack, i);
        inputStack(&tmpstack, i + 10000);//入栈2个出栈1个。
        if(getStack(&tmpstack, &tmpvalue) < 0)
        {
            continue;
        }
        logdebugc("get value = %d", tmpvalue);
    }

    logdebugc("pop stack data--------------------->");

    while(!(getStack(&tmpstack, &tmpvalue) < 0))
    {
        logdebugc("get value = %d", tmpvalue);
    }

    freeStack(&tmpstack);
    return 1;
}



